//
//  WelcomeController.m
//
//  Created by pakeyfan on 12-6-29.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "WelcomeController.h"
#import "EJournalsAppDelegate.h"
//#import "UIKitCategories.h"

@implementation WelcomeController
- (void)initPage
{
	lastView = nil;
	mainView = nil;
	nextView = nil;
	pageControl.currentPage = 0;
	[self pageChange];
}

- (UIPageControl*)getPageControl
{
	return pageControl;
}

- (void) pageChange{
	//position
	CGRect parentViewBound = [self view].frame;
	CGRect newRect = CGRectMake(parentViewBound.origin.x, 
								parentViewBound.origin.y, 
								parentViewBound.size.width, 
								parentViewBound.size.height);
	firstView.frame = newRect;
	secondView.frame = newRect;
	thirdView.frame = newRect;
	forthView.frame = newRect;
	fifthView.frame = newRect;
	blank.frame = newRect;
	blank.backgroundColor = [UIColor blackColor];
	[firstView removeFromSuperview];
	[secondView removeFromSuperview];
	[thirdView removeFromSuperview];
	[forthView removeFromSuperview];
	[fifthView removeFromSuperview];
	
		
	
	//[UIView commitAnimations];
    switch([pageControl currentPage]){
		case 0:
			[[self view] addSubview:firstView];
			[[self view] addSubview:secondView];
			[[self view] bringSubviewToFront:firstView];
			lastView = nil;
			mainView = firstView;
			nextView = secondView;
			break;
		case 1:
			
			[[self view] addSubview:firstView];
			[[self view] addSubview:secondView];
			[[self view] addSubview:thirdView];
			[[self view] bringSubviewToFront:secondView];
			lastView = firstView;
			mainView = secondView;
			nextView = thirdView;
			break;
		case 2:
			
			[[self view] addSubview:secondView];
			[[self view] addSubview:thirdView];
			[[self view] addSubview:forthView];
			[[self view] bringSubviewToFront:thirdView];
			lastView = secondView;
			mainView = thirdView;
			nextView = forthView;
			break;
		case 3:	
			[[self view] addSubview:thirdView];
			[[self view] addSubview:forthView];
			[[self view] addSubview:fifthView];
			[[self view] bringSubviewToFront:forthView];
			lastView = thirdView;
			mainView = forthView;
			nextView = fifthView;
			break;
		case 4:
			[[self view] addSubview:forthView];
			[[self view] addSubview:fifthView];
			[[self view] bringSubviewToFront:fifthView];
			lastView = forthView;
			mainView = fifthView;
			nextView = nil;
			break;
		default:
			break;
	}
	[[self view] bringSubviewToFront:pageControl];
}

- (void) pageChangeWithAnimation:(int) direction
{
	[UIView beginAnimations:@"animationID" context:nil];
	[UIView setAnimationDuration:1.5f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	//prepare animation
	if (mainView!=nil) {
		if (direction == 1) {
			[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
		}else {
			[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:mainView cache:YES];
		}
	}
	[self pageChange];
	[UIView commitAnimations];
}

- (void)moveCurrentStartNavigateView:(float)dx
{
	[[self view] bringSubviewToFront: mainView];
	if (dx<0) {
		if (nextView != nil) {
			CGRect mainOldRect = mainView.frame;
			CGRect mainNewRect = CGRectMake(dx, 
											mainOldRect.origin.y, 
											mainOldRect.size.width, 
											mainOldRect.size.height);
			mainView.frame = mainNewRect;
			
			CGRect nextOldRect = nextView.frame;
			CGRect nextNewRect = CGRectMake([[self view] bounds].size.width + dx, 
											nextOldRect.origin.y,
											nextOldRect.size.width,
											nextOldRect.size.height);
			nextView.frame = nextNewRect;
			[[self view] bringSubviewToFront:blank];
			[[self view] bringSubviewToFront:mainView];
			[[self view] bringSubviewToFront:nextView];
			[[self view] bringSubviewToFront:pageControl];
		}
	}else {
		if(lastView != nil){
			CGRect mainOldRect = mainView.frame;
			CGRect mainNewRect = CGRectMake(dx, 
											mainOldRect.origin.y, 
											mainOldRect.size.width, 
											mainOldRect.size.height);
			mainView.frame = mainNewRect;
			
			CGRect lastOldRect = lastView.frame;
			CGRect lastNewRect = CGRectMake(-[[self view] bounds].size.width + dx, 
											lastOldRect.origin.y,
											lastOldRect.size.width,
											lastOldRect.size.height);
			lastView.frame = lastNewRect;
			[[self view] bringSubviewToFront:blank];
			[[self view] bringSubviewToFront:mainView];
			[[self view] bringSubviewToFront:lastView];
			[[self view] bringSubviewToFront:pageControl];
		}
	}

}

@end

@implementation StartNavigateView
- (id) initWithImage:(UIImage *)image
{
	if(self = [super initWithImage:image])
		self.userInteractionEnabled = YES;
	return self;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	CGPoint pt = [[touches anyObject] locationInView:[self superview]];
	startLocation = pt;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	CGPoint pt = [[touches anyObject] locationInView:[self superview]];
	dx = pt.x - startLocation.x;
	WelcomeController *viewControler = (WelcomeController*)[[self nextResponder] nextResponder];
	[viewControler moveCurrentStartNavigateView:dx];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	WelcomeController *viewControler = (WelcomeController*)[[self nextResponder] nextResponder];

	UIPageControl *pc = [viewControler getPageControl];
	NSInteger curPage = pc.currentPage;
	if (dx<-50) { //right move
		if(curPage+1==pc.numberOfPages){
			EJournalsAppDelegate* ejad = (EJournalsAppDelegate*)[[UIApplication sharedApplication] delegate];
			[ejad.window addSubview:ejad.tabBarController.view];
			[viewControler.view removeFromSuperview]; 
			return;
		}else{
			pc.currentPage = curPage + 1;
			[viewControler pageChangeWithAnimation:1];
			return;
		}
	}else if(dx>50){   //left move
		if (curPage>0) {
			pc.currentPage = curPage - 1;
			[viewControler pageChangeWithAnimation:-1];
			return;
		}
	}
	[viewControler pageChange];
}

@end

